<?php

FLEA::loadClass ( 'Common_Action' );

class Controller_ArticleCatalog extends Common_Action {
	
	private $_modelCatalog = null;
	
	function __construct($ctlName) {
		parent::__construct ( $ctlName );
		$this->tplDir = TPL_A;
		$this->_modelCatalog = FLEA::getSingleton ( 'Model_ArticleCatalog' );
	}
	
	function index() {
		$viewData = $this->_modelCatalog->renderAllCatalogs ();
		//dump($viewData);
		$this->_out ( 'listCatalogs', $viewData );
	}
	
	function insertCatalog() {
		if ($this->_isPOST ()) {
			$result = $this->_modelCatalog->insertCatalog ( $_POST );
			if ($result != NULL) {
				js_alert ( '添加成功', null, url ( 'articlecatalog' ) );
			}
		} else {
			$viewData = array ('form' => $this->_modelCatalog->getForm (), 'action' => 'insertCatalog' );
			$this->_out ( 'insertCatalog', $viewData );
		}
	}
	
	function modifyCatalog() {
		if ($this->_isPOST ()) {
			$result = $this->_modelCatalog->updateCatalog($_POST);
			if ($result != NULL) {
				js_alert ( '修改成功', null, url ('articlecatalog') );
			}	
		} else {
			$catalogId = $this->saveGet ( 'catalog' );
			$catalog = $this->_modelCatalog->getCatalogById ( $catalogId );
			$viewData = array ('form' => $this->_modelCatalog->getForm ( $catalog ), 'action' => 'modifyCatalog' );
			//dump($viewData);
			$this->_out ( 'insertCatalog', $viewData );
		}
	}
	
	function deleteCatalog() {
		//询问是否删除
		$catalogId = $this->saveGet ( 'catalog' );
		$this->_modelCatalog->deleteCatalogById($catalogId);
		js_alert ( '删除成功', null, url ('articlecatalog') );
	}

}
